// pages/appointmentRecord/appointmentRecord.js
import * as API_Appointment from '../../api/appointment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordList:[],
    params:{
      pageSize:20,
      pageNum:1,
    },
    finished:false,
  },
  goStore(e){
    wx.navigateTo({
      url: '/pages/storeHome/storeHome?id='+e.currentTarget.dataset.id,
    })
  },
  cancelAppointment(e){
    let id=e.currentTarget.dataset.id;
    let that=this;
    wx.showModal({
      title:'提示',
      content:'确定取消此次预约？',
      success (res) {
        if (res.confirm) {
          that.cancel(id);
        }
      }
    })
  
  },
  cancel:function(id){
   
    let params={id:id};
    let that=this;
    API_Appointment.cancelAppointment(params).then(res=>{
      wx.showToast({title: res.msg, icon:'none', duration:1500})
      if(res.code==200){
        
        setTimeout(() => {
          that.flustList();
        }, 1500);
       
      }
    })
  },
  flustList:function(){
    this.setData({'params.pageNum':1,recordList:[]});
    this.listRecord();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.listRecord();
  },
  listRecord:function(){
    API_Appointment.listRecord(this.data.params).then(res=>{
      let data=res.data;
      let recordList=this.data.recordList;
      recordList.push(...data.rows);
       this.setData({recordList:recordList, finished:data.lastPage})
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.finished){
      return
    }
    var page = this.data.params.pageNum + 1
    this.setData({
      'params.pageNum':page
    })
    this.listRecord();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})